package controller

import (
	"GoAdmin/common"
	"GoAdmin/models"
	"log"
	"net/http"
	"strconv"
	"time"

	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"xorm.io/builder"
)

func SchoolList(c *gin.Context) {

	user, _ := common.GetCtxUser(c)
	filter := builder.NewCond()
	if user.Type == "0" || user.Type == "1" {
		log.Println("管理用户")
		//管理用户查询全部
	} else {
		if user.Province != "" {
			filter = filter.And(builder.Eq{"province": user.Province})
		}
		if user.City != "" {
			filter = filter.And(builder.Eq{"city": user.City})
		}
		if user.District != "" {
			filter = filter.And(builder.Eq{"district": user.District})
		}
	}
	if user.Type == "3" {
		//同一个地区双代理
		filter = filter.And(builder.In("config", user.SchoolList))
	}
	count, list, _ := new(models.School).GetSchoolListByFilter(filter)
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "ok", "data": gin.H{"schoolList": list, "count": count}})
	return
}

func SchoolDetail(c *gin.Context) {
	var (
		schoolId string
	)
	schoolId = c.PostForm("school_id")
	valid := validation.Validation{}
	valid.Required(schoolId, "school_id").Message("未选择驾校")
	if valid.HasErrors() {
		for _, err := range valid.Errors {
			c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Message, "data": nil})
			return
		}
	} else {
		school, _ := new(models.School).GetSchoolById(schoolId)
		c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "ok", "data": school})
		return
	}
}

func CityList(c *gin.Context) {
	user, _ := common.GetCtxUser(c)
	filter := builder.NewCond()
	if user.Type == "0" || user.Type == "1" {
		log.Println("管理用户")
		//管理用户查询全部
	} else {
		if user.Province != "" {
			filter = filter.And(builder.Eq{"province": user.Province})
		}
		if user.City != "" {
			filter = filter.And(builder.Eq{"city": user.City})
		}
		if user.District != "" {
			filter = filter.And(builder.Eq{"district": user.District})
		}
	}
	if user.Type == "3" {
		//同一个地区双代理
		filter = filter.And(builder.Eq{"config": user.SchoolList})
	}
	list, _ := new(models.City).GetCity(filter)
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "ok", "data": gin.H{"cityList": list}})
	return
}

func AllSchoolList(c *gin.Context) {
	filter := builder.NewCond()
	user, _ := common.GetCtxUser(c)
	if user.Type == "3" {
		//同一个地区双代理
		filter = filter.And(builder.Eq{"config": user.SchoolList})
	}
	if c.PostForm("school_name") != "" {
		filter = filter.And(builder.Like{"name", c.PostForm("school_name")})
	}
	list, _ := new(models.School).GetSchoolByFilter(filter)
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "ok", "schoolList": list})
	return
}

func AddSchoolNumber(c *gin.Context) {
	var (
		schoolId string
		userId   string
	)
	schoolId = c.PostForm("school_id")
	userId = c.PostForm("user_id")
	valid := validation.Validation{}
	valid.Required(schoolId, "school_id").Message("未选择驾校")
	valid.Required(userId, "user_id").Message("该用户不存在")
	if valid.HasErrors() {
		for _, err := range valid.Errors {
			c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Message, "data": nil})
			return
		}
	}
	user, _ := new(models.OaUser).GetByUser(c.PostForm("user_id"))
	usedNumber := user.Usednum + cast.ToInt(c.PostForm("count"))
	if (user.Registernum - usedNumber) < 0 {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "管理员账户不足", "data": nil})
		return
	}
	school, _ := new(models.School).GetSchool(schoolId)
	if user.Id != 1 && user.Id != 49 {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "失败", "data": nil})
		return
	}
	//减少代理账号，记录日志
	if !user.UpdateNumber(user.Id, usedNumber) {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "失败", "data": nil})
		return
	}
	order := new(models.OaOrder)
	order.SchoolId = cast.ToInt(c.PostForm("school_id"))
	order.UserId = cast.ToInt(c.PostForm("user_id"))
	order.Number = cast.ToInt(c.PostForm("count"))
	order.Addtime = time.Now()
	order.Save()
	school.Registernum = school.Registernum + cast.ToInt64(c.PostForm("count"))
	if !school.EditSchool("registernum") {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "失败", "data": nil})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "成功", "data": nil})
	return
}

func PayOrderList(c *gin.Context) {

	page, _ := strconv.Atoi(c.DefaultPostForm("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultPostForm("pageSize", "20"))
	offset := (page - 1) * pageSize

	list, count, err := new(models.OaOrder).GetPayStudentList(c.PostForm("user_id"), pageSize, offset)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "失败", "data": nil})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "成功", "data": gin.H{"data": list, "total": count}})
	return
}
